# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=glbinding
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-angleproject")
pkgver=3.3.0
pkgrel=1
pkgdesc="A C++ binding for the OpenGL API, generated using the gl.xml specification (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/cginternals/glbinding'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/cginternals/glbinding/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        'mingw-unix-layout.patch'
        'config-export-location.patch')
sha256sums=('a0aa5e67b538649979a71705313fc2b2c3aa49cf9af62a97f7ee9a665fd30564'
            '454582287a2ec3cb953d63b3c90507e5d25abded0b032bd9a120333d9c0bc567'
            '3340339a2f5c1c0c660d169dea964579bc3f203c08ba81f10fce4406604f3e1a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    mingw-unix-layout.patch \
    config-export-location.patch 
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOPTION_BUILD_EXAMPLES=OFF \
    -DOPTION_BUILD_TOOLS=OFF \
    -DOPTION_BUILD_TESTS=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DOPTION_BUILD_DOCS=OFF \
    -DSYSTEM_DIR_INSTALL=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
